/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cliente.java
 *
 * Created on 15/04/2010, 11:21:45
 */
package user_gui;

import elemento.ClienteTelefone;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Jônatas
 */
public class EditaClienteFromBusca extends javax.swing.JFrame implements ContainsEmpresa {

    private String telefoneSimples;
    private String telefoneCompleto;
    private String nome;
    private FramePrincipal parentComponent;
    private ClienteTelefone old;
    private boolean origem;

    /** Creates new form Cliente */
    public EditaClienteFromBusca(String telefoneSimples, String telefoneCompleto, FramePrincipal parentComponent, String nome, boolean origem) {
        this.telefoneSimples = telefoneSimples;
        this.telefoneCompleto = telefoneCompleto;
        this.parentComponent = parentComponent;

        try {
            old = FramePrincipal.COMUNICABD.select(FramePrincipal.tableName, telefoneSimples);
        } catch (SQLException ex) {
            Logger.getLogger(EditaClienteFromBusca.class.getName()).log(Level.SEVERE, null, ex);
        }

        initComponents();

        jTextField1.setText(nome);
        jLabel3.setText(this.telefoneCompleto);
        this.parentComponent.setEnabled(false);
        this.origem = origem;
        this.tfEmpresa.setText("" + old.getId_empresa());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        tfEmpresa = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Nome:");

        jLabel2.setText("Telefone:");

        jLabel3.setText(" ");

        jButton1.setText("Excluir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Alterar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton5.setText("...");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel4.setText("Empresa:");

        tfEmpresa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfEmpresaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(tfEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(jButton5))
                        .addContainerGap())))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-401)/2, (screenSize.height-150)/2, 401, 150);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

        this.parentComponent.setEnabled(true);

    }//GEN-LAST:event_formWindowClosing

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        nome = jTextField1.getText();
        String id = tfEmpresa.getText();

        ClienteTelefone cliente = new ClienteTelefone(nome, telefoneSimples);
        if (!id.isEmpty()) {
            cliente.setId_empresa(Integer.parseInt(id));
        }
        try {
            if (FramePrincipal.COMUNICABD.update(FramePrincipal.tableName, cliente, old)) {
                JOptionPane.showMessageDialog(this, "O cadastro foi alterado com sucesso.");
                parentComponent.setEnabled(true);
                if (this.origem) {
                    this.parentComponent.atualizaOrigem(this.telefoneSimples);
                } else {
                    this.parentComponent.atualizaDestino(this.telefoneSimples);
                }
            } else {
                JOptionPane.showMessageDialog(this, "Ocorreu um erro durante o cadastro.");
                parentComponent.setEnabled(true);
            }
        } catch (SQLException ex) {
            Logger.getLogger(FramePrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            dispose();
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        int resposta = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja excluir o registro?",
                "ATENÇÃO", JOptionPane.YES_NO_OPTION);

        if (resposta == 0) {
            try {
                boolean b = FramePrincipal.COMUNICABD.delete(FramePrincipal.tableName, telefoneSimples);

                if (b) {
                    JOptionPane.showMessageDialog(this, "A entrada foi removida com sucesso.");

                    parentComponent.setEnabled(true);
                    if (this.origem) {
                        this.parentComponent.atualizaOrigem(this.telefoneSimples);
                    } else {
                        this.parentComponent.atualizaDestino(this.telefoneSimples);
                    }
                    this.dispose();
                    return;
                }

            } catch (SQLException ex) {
                Logger.getLogger(EditaClienteFromBusca.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        ListaEmpresas empresas = new ListaEmpresas(this);
        empresas.setVisible(true);
}//GEN-LAST:event_jButton5ActionPerformed

    private void tfEmpresaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfEmpresaKeyReleased
        // TODO add your handling code here:
}//GEN-LAST:event_tfEmpresaKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField tfEmpresa;
    // End of variables declaration//GEN-END:variables

    public void setEmpresaText(String idEmpresa) {
        tfEmpresa.setText(idEmpresa);
    }
}
